package com.coldchain.admin.user.api;

import com.coldchain.admin.user.entity.Admin;
import com.coldchain.admin.user.service.IAdminService;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.util.MD5Utils;
import com.fangliai.core.context.BaseContextHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/user")
@CheckUserToken
public class AdminController {
    private static final Logger log= LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private IAdminService adminService;

    @PostMapping("/add")
    public  Object add(Admin admin){
        try{
            admin.setCreateBy(BaseContextHandler.getUsername());
            admin.setCreateTime(new Date());
            admin.setLoginPwd(MD5Utils.getMD5Str(admin.getLoginPwd()));
            adminService.save(admin);
            return new ObjectRestResponse<>().succeed("ok");
        }catch (Exception e){
            log.error("添加管理员失败!error[{}]",e.getMessage());
            return new ObjectRestResponse<>().error("服务器异常");
        }

    }

    @GetMapping("/list")
    public Object list(){
        try{
            return new ObjectRestResponse<>().data(adminService.getAdminVo()).succeed();
        }catch (Exception e){
            log.error("获取管理员失败!error[{}]",e.getMessage());
            return new ObjectRestResponse<>().error("查询失败");
        }
    }
}
